/*
 *	Copyright © 2013 Changsha Shishuo Network Technology Co., Ltd. All rights reserved.
 *	长沙市师说网络科技有限公司 版权所有
 *	http://www.shishuo.com
 */
package com.shishuo.cdn.action;

import java.io.UnsupportedEncodingException;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;

import com.shishuo.cdn.dao.UserDao;
import com.shishuo.cdn.entity.User;
import com.shishuo.cdn.entity.vo.JsonVo;

/**
 * 
 * @author Herbert
 * 
 */
public class BaseAction {
	protected BaseAction(){
//		System.out.println(ConfigUtil.getProjectHttpUrl());
//		RequestAttributes ra=RequestContextHolder.getRequestAttributes();
//		if(ra!=null){
//			HttpServletRequest request =  ((ServletRequestAttributes)ra).getRequest();
//			paramsMap=createParameterMap(request);
//		}
	}
	protected JsonVo json=new JsonVo();
	protected Map<String,Object> paramsMap=new HashMap<String,Object>();
	
	protected final Logger logger = Logger.getLogger(this.getClass());
	
	protected Map<String,Object> createParameterMap(HttpServletRequest request){
		json=new JsonVo();
		paramsMap=new HashMap<String,Object>();
		try {
			request.setCharacterEncoding("utf-8");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		Enumeration<String> paramKeys=request.getParameterNames();
		while(paramKeys.hasMoreElements()){
			String key=paramKeys.nextElement();
			try {
				System.out.println("--------------------->"+ request.getParameter(key)+"--------------------->"+new String(request.getParameter(key).getBytes("ISO-8859-1"),"UTF-8"));
				paramsMap.put(key, new String( request.getParameter(key).getBytes("ISO-8859-1"),"UTF-8"));
//				paramsMap.put(key, request.getParameter(key));
			} catch (UnsupportedEncodingException e) {
				e.printStackTrace();
			}
		}
		
		if(request.getSession().getAttribute("user")!=null)//用户id和用户对象
		{
			User user=(User)(request.getSession().getAttribute("user"));
			paramsMap.put("user_id", user.getUser_id());
			paramsMap.put("userObj", user);
		}
		return paramsMap;
	}
	@Autowired
	private UserDao userDao;
	/**
	 * 刷新session里的user对象，一般是用户资金变动后操作 ,修改密码 2015年8月20日 10:02:34
	 */
	protected void refreshSessionUser(HttpServletRequest request){
		User user=userDao.getUserById(((User)request.getSession().getAttribute("user")).getReg_tel());
		request.getSession().setAttribute("user", user);
	}
}
